package com.example.server.controller;

import ch.qos.logback.core.util.TimeUtil;
import com.example.server.redis.RedisService;
import com.example.server.util.Util;
import com.example.server.entity.User;
import com.example.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
public class UserController extends BaseController{
    private final UserService userService;
    @Autowired
    private RedisService redisService;
    private RedisTemplate<String,String> redisTemplate;

//    public UserController(UserService userService) {
//        this.userService = userService;
//    }

    public UserController(UserService userService, RedisTemplate<String,String> redisTemplate) {
        this.userService = userService;
        this.redisTemplate = redisTemplate;
    }

    //注册用户
    @PostMapping("create")
    private int create(@RequestParam MultipartFile file,@RequestPart User user){
        String directory = "D:/softwear/springboot-team03/photo/avatar";
        user.setAvatar(Util.savePicture(file,directory));
        return userService.create(user);
    }

    //用户登录
    @PostMapping("queryByAccountAndPassword")
    private User login(@RequestBody User user){
        User user1 = userService.queryOne("queryByAccountAndPassword",user);
        if(user1 != null){
            redisService.setString("uid", String.valueOf(user1.getId()));
            int id = Integer.parseInt(redisService.getSetString("uid" ));
            //            redisTemplate.delete("userId");
//            redisTemplate.opsForValue().set("userId",user1.getId());
//            int id = (int) redisTemplate.opsForValue().get("userId
//            redisTemplate.opsForValue().set("uid", String.valueOf(user1.getId()));
//            int id = Integer.parseInt(redisTemplate.opsForValue().get("uid"));
            System.out.println("==========================================================================z" +id);
            String token = Util.generate(String.valueOf(user1.getId()));
            user1.setToken(token);
            return user1;
        }else {
            System.out.println("登录失败");
            return null;
        }
    }

//    查询用户个人主页
    @GetMapping("queryById")
    private User queryById(){
            int id = Integer.parseInt(redisService.getSetString("uid"));
            User user1 = userService.queryOne("queryById",id);
            return user1;

    }
    @GetMapping("queryById/{id}")
    private User queryById(@PathVariable int id){
//            int id = Integer.parseInt(redisService.getSetString("uid"));
            User user1 = userService.queryOne("queryById",id);
            return user1;

    }

    //更改用户信息
    @PutMapping("modify")
    private int modify(@RequestPart MultipartFile file,@RequestPart User user){
        int id = Integer.parseInt(redisService.getSetString("uid" ));
        System.out.println(id);
        String directory = "D:/softwear/springboot-team03/photo/avatar";
        user.setId(id);
        user.setAvatar(Util.savePicture(file,directory));

        return userService.modify(user);
    }

    //更改密码
    @PutMapping("modifyPassword")
    private int modifyPassword(@RequestBody User user){
        int id = Integer.parseInt(redisService.getSetString("uid" ));
        user.setId(id);
        return userService.modifyOne("modifyPassword",user);
    }

    //退出登录
    @PostMapping("signOut")
    public boolean logout() {
        System.out.println("===========================12323");
        String token = request.getHeader("Authorization");
        String encryptedToken = DigestUtils.md5DigestAsHex(token.getBytes());
        long remainedTime = Util.getRemainedTime(token);
        String id = Util.getSubject(token);
        if(remainedTime > 0){
            redisTemplate.opsForValue().set(
                    encryptedToken,
                    id,
                    remainedTime,
                    TimeUnit.SECONDS
                    );
        }
        return true;
    }
}
